package zy.blue7.im.common.serializable.impl;

import zy.blue7.im.common.serializable.SerializableHandler;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author blue7
 * @version 1.0
 * @date 2022/3/1 15:58
 */
public class JavaSerializableHandlerImpl implements SerializableHandler {
    @Override
    public byte[] serializable(Object obj) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        return baos.toByteArray();
    }

    @Override
    public Object dSerializable(byte[] src) throws Exception {
        ByteArrayInputStream baos = new ByteArrayInputStream(src);
        ObjectInputStream oos = new ObjectInputStream(baos);

        return oos.readObject();
    }
}
